package cn.neusoft.controller;


import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.po.User;
import cn.neusoft.service.IUserService;
import cn.neusoft.util.TokenUtil;
import cn.neusoft.vo.ResponseObj;
import cn.neusoft.vo.loginVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hxj
 * @since 2023-12-28
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * 自动装配redis的工具类
     */
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 手机号注册
     * @param user
     * @return
     */
    @PostMapping("/phoneReg")
    public ResponseObj phoneReg(@RequestBody User user){

        //参数校验
        if (!StringUtils.hasText(user.getPhone())){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        if (!StringUtils.hasText(user.getPassword())){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        //密码加密
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        //设置默认状态为激活状态
        user.setState(1);
        //设置创建时间
        user.setCreatetime(new Date());

        //注册就是添加
        boolean save = userService.save(user);

        return save?ResponseObj.SUCCESS(user.getId()):ResponseObj.ERROR(12001,"注册失败");
    }

    /**
     * 修改个人信息
     * @param user
     * @return
     */
    @PutMapping
    public ResponseObj update(@RequestBody User user){
        //主键为空无法修改
        if (user.getId()==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        boolean b = userService.updateById(user);
        return b?ResponseObj.SUCCESS():ResponseObj.ERROR(12005,"修改用户信息失败");
    }

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public ResponseObj login(@RequestBody loginVo loginVo){

        //通过账号查询到的用户对象
        User loginUser = userService.login(loginVo);

        //比对密码
        if(passwordEncoder.matches(loginVo.getPassword(),loginUser.getPassword())){
            //生成token
            String token = TokenUtil.sign(loginUser.getId().toString());
            //存redis
            redisTemplate.opsForValue().set(token,loginUser,30, TimeUnit.MINUTES);

            loginUser.setPassword(null);
            return ResponseObj.SUCCESS(token,loginUser);
        }
        return ResponseObj.ERROR(12004,"用户密码错误");
    }



}
